<?php
// +----------------------------------------------------------------------
// | snake
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2022 http://baiyf.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: NickBai <1902822973@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\model;

use think\Model;

class CompanyModel extends Model
{
    // 确定链接表名
    protected $table = 'cbdd_company';
    protected $pk = 'id';

    /**
     * 根据搜索条件获取用户列表信息
     * @param $where
     * @param $offset
     * @param $limit
     */
    public function getCompanyByWhere($where, $offset, $limit)
    {
        return $this->alias('a')->field($this->table  . '.*,b.name bname')->join('cbdd_company b ','a.top_cid = b.id')
            ->where($where)->limit($offset, $limit)->order('id desc')->select();
    }
    /**
     * [user 关联模型]
     * @return [type] [description]
     */
    public function user()
    {
        return $this->hasOne('UserModel');
    }

    public function getUserType($id){
        $where['b.id'] = $id;
        return $this->alias('a')->field('b.*,a.id aid')->join('cbdd_user b ','b.user_name = a.mobile','right')->where($where)->find();
    }

    /**
     * 根据搜索条件获取所有的用户数量
     * @param $where
     */
    public function getAllCompany($where)
    {
        return $this->alias('a')->where($where)->count();
    }
    /**
     * [getTopCompany 获取上级代理]
     * @return [type] [description]
     */
    public function getTopCompany($id,$role_id)
    {
        if($role_id == 3){
            $map['top_cid'] = $id;
        }else{
            $map['top_cid'] = ['gt',0];
        }
        $where['role'] = 3;
        $where['id'] = $id;
        $where['status'] = 1;
        return $this->where($where)->whereor($map)->select();
    }

    public function getAllCouponInfo($cid){
        $data['bid'] = $this->alias('a')->join('cbdd_coupon_type c ','a.id = c.cid')
        ->join('cbdd_coupon b ','c.id = b.type')
        ->where('b.status',1)
        ->where('c.cid',$cid)
        ->column('b.id');

        $data['rows'] = $this->alias('a')->join('cbdd_coupon_type c ','a.id = c.cid')
        ->join('cbdd_coupon b ','c.id = b.type')
        ->where('b.status',1)
        ->where('c.cid',$cid)
        ->select();
        return $data;
    }
    /**
     * [getTopCompanyInfo 获取上级代理信息]
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function getTopCompanyInfo($id){
        $where['id'] = $id;
        return $this->where($where)->find();
    }

    /**
     * 插入管理员信息
     * @param $param
     */
    public function insertCompany($param)
    {
            $result =  $this->validate('CompanyValidate.add')->save($param);
            if($result){
                // 验证失败 输出错误信息
                return true;
            }else{
                return false;
            }
    }

    /**
     * 编辑管理员信息
     * @param $param
     */
    public function editUser($param)
    {
        try{
            $result =  $this->save($param, ['id' => $param['id']]);
            if(false === $result){
                // 验证失败 输出错误信息
                return msg(-1, '', $this->getError());
            }else{
                return msg(1, url('company/index'), '编辑用户成功');
            }
        }catch(PDOException $e){
            return msg(-2, '', $e->getMessage());
        }
    }

    /**
     * 根据管理员id获取角色信息
     * @param $id
     */
    public function getOneUser($id)
    {
        return $this->where('id', $id)->find();
    }
    /**
     * [switchCompany 启用 禁用]
     * @return [type] [description]
     */
    // public function switchCompany($id){
    //     try{

    //         // $this->where('id', $id)->delete();
    //         // $this->('id',$id)->save('status');
    //         if($status == 1){
    //             return msg(1, '', '关闭管理员成功');
    //         }else{
    //             return msg(1, '', '开启管理员成功');
    //         }

    //     }catch( PDOException $e){
    //         return msg(-1, '', $e->getMessage());
    //     }
    // }

    /**
     * 删除管理员
     * @param $id
     */
    public function delUser($id)
    {
        try{

            $this->where('id', $id)->delete();
            return msg(1, '', '删除管理员成功');

        }catch( PDOException $e){
            return msg(-1, '', $e->getMessage());
        }
    }

    /**
     * 根据用户名获取管理员信息
     * @param $name
     */
    public function findUserByName($name)
    {
        return $this->where('user_name', $name)->find();
    }

    /**
     * 更新管理员状态
     * @param array $param
     */
    public function updateStatus($param = [], $uid)
    {
        try{

            $this->where('id', $uid)->update($param);
            return msg(1, '', 'ok');
        }catch (\Exception $e){

            return msg(-1, '', $e->getMessage());
        }
    }
}